package com.wsc.springbootmultidevdemo.msg.dlx;

import com.rabbitmq.client.*;
import com.wsc.springbootmultidevdemo.msg.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 死信队列监听
 */
public class DlxListener {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        String exchange = "dlx.exchange";
        String queue = "dlx.queue";
        String routingKey = "#";

        channel.queueDeclare(queue, false, false, false, null);
        channel.exchangeDeclare(exchange, BuiltinExchangeType.TOPIC);
        channel.queueBind(queue, exchange, "#");

        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg  = new String(body,"utf-8");
                System.out.println("receive a dead letter :" + msg);
            }
        };
        channel.basicConsume(queue, true, consumer);
    }
}
